package com.tarena.lbs.business.servcie.ipml;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.attach.api.AttachApi;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.dao.repository.StoreRepository;
import com.tarena.lbs.business.pojo.param.StoreParam;
import com.tarena.lbs.business.pojo.po.StorePO;
import com.tarena.lbs.business.pojo.query.AreaStoreQuery;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import com.tarena.lbs.business.pojo.vo.StoreVO;
import com.tarena.lbs.business.servcie.StoreService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class StoreServiceImpl implements StoreService {
    @Autowired
    private StoreRepository  storeRepository;
  @DubboReference
  private AttachApi attachApi;
    @Override
    public PageResult<StoreVO> storeList(StoreQuery query) {
        log.info("查询店铺列表{}",query);
        PageResult<StoreVO> result=new PageResult<>(query.getPageSize(),query.getPageNo());
        PageInfo<StorePO> pageInfo=getPageBusiness(query);
        result.setTotal(pageInfo.getTotal());
        List<StorePO> pos = pageInfo.getList();
        List<StoreVO> vos=null;
        if (pos!=null&& pos.size()>0){
            vos=pos.stream().map(po->{
                StoreVO vo=new StoreVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        result.setObjects(vos);
        return result;
    }
    private PageInfo<StorePO> getPageBusiness(StoreQuery query){
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        log.info("查询店铺列表页数{},{}",query.getPageNo(), query.getPageSize());
        log.info("查询店铺列表{}",query);
        List<StorePO> pos = storeRepository.list(query);
        return new PageInfo<>(pos);
    }

    @Override
    public void add(StoreParam param) {
        log.info("新增店铺{}",param);
        StorePO  storePO = new StorePO();
        BeanUtils.copyProperties(param,storePO);
      //  storePO.setStoreImagesId(param.getStoreImagesIds().toString());
        storeRepository.add(storePO);

    }

    @Override
    public PageResult<StoreVO> getStoreByCity(AreaStoreQuery query) {
        log.info("查询区域下的店铺列表: {}", query);
        return storeRepository.getStoreByCity(query);
    }

    @Override
    public void location(StoreVO locationPO) {
        log.info("手机小程序定位上报{}",locationPO);
        storeRepository.location(locationPO);
    }

//    @Override
//    public PageResult<StoreVO> getStoreByName(StoreQuery query) {
//        log.info("模糊查询的店铺列表: {}", query);
//        return storeRepository.getStoreByName(query);
//    }

    @Override
    public int delete(Integer id) {
        log.info("删除店铺: {}", id);
        return storeRepository.delete( id);

    }

    @Override
    public int updateStatus(Integer id, Integer status) {
        int count = storeRepository.updateStatus(id, status);
        return count;
    }

}
